﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.DataFetcher;

namespace AgencyManagerSystem.Core
{
    public class CombinationUpdaterCreator : MainDetailUpdaterCreator<CombinationModel, ElementModel>
    {

        CombinationFetcher combinationFetcher = new CombinationFetcher();
        ElementFetcher elementFetcher = new ElementFetcher();
        public CombinationUpdater CreateCombinationUpdaterInstance()
        {
            var mainUpdater = CombinationUpdater.GetMainUpdater();
            mainUpdater.DataUpdating += FlagAndApproveFlagValid;
            mainUpdater.DataUpdating += ProductionExists;
            mainUpdater.DataDeleting += OrderExists;
            CombinationUpdater combinationUpdater = new CombinationUpdater(combinationFetcher, elementFetcher, mainUpdater);
            combinationUpdater.DataBulkUpdating += CommonValid;
            combinationUpdater.DataBulkUpdating += CodeUniqueAndNotSpace;
            combinationUpdater.DataBulkUpdating += DetailCheck;
            return combinationUpdater;
        }

        private void OrderExists(BaseUpdater<CombinationModel> sender, DataDeletingEventArgs<CombinationModel> e)
        {
            if (e.Delete == null) return;
            var orderDetailFetcher = new OrderDetailFetcher();
            var ls = orderDetailFetcher.List(new string[] { "Combination", e.Delete.AutoId.ToString() });
            if (ls.Count > 0)
            {
                e.Message = String.Format("组合{0}已存在于订单明细行{1}", e.Delete.AutoId, ls.First().AutoId);
                e.Cancel = true;
                return;
            }
        }

        private void ProductionExists(BaseUpdater<CombinationModel> sender, DataUpdatingEventArgs<CombinationModel> e)
        {
            if (e.Insert == null)
                return;
            var productionFetcher = new ProductionFetcher();
            var production = productionFetcher.Get(e.Insert.Production);
            if(production==null||!production.Combination)
            {
                e.Message = String.Format("产品{0}不存在或者不是组合型产品",e.Insert.Production);
                e.Cancel = true;
                return;
            }
        }

        private void DetailCheck(MainDetailUpdater<CombinationModel, ElementModel> sender, DataBulkUpdatingEventArgs<CombinationModel, ElementModel> e)
        {
            if(e.DetailInsert==null)
            {
                return;
            }
            if(e.DetailInsert.Count()==0)
            {
                e.Message = String.Format("组合{0}没有包含产品明细项", e.Insert.Code);
                e.Cancel = true;
                return;
            }
            foreach(var item in e.DetailInsert)
            {
                if(item.Quantity<=0)
                {
                    e.Message = String.Format("组合{0}产品组成数量必须大于0", e.Insert.Code);
                    e.Cancel = true;
                    return;
                }
                ProductionFetcher productionFetcher = new ProductionFetcher();
                if (productionFetcher.Get(item.Production)==null)
                {
                    e.Message = String.Format("组合{0}有不存在的产品{1}", e.Insert.Code,e.Insert.Production);
                    e.Cancel = true;
                    return;
                }
            }
        }
        private void CodeUniqueAndNotSpace(MainDetailUpdater<CombinationModel, ElementModel> sender, DataBulkUpdatingEventArgs<CombinationModel, ElementModel> e)
        {
            if (e.Insert == null)
                return;
            if (String.IsNullOrWhiteSpace(e.Insert.Code) || String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Cancel = true;
                e.Message = String.Format("单据{0}编号或名称不能为空", e.Insert.AutoId);
                return;
            }
            var combination = combinationFetcher.GetByCode(e.Insert.Code);
            if (combination != null && combination.AutoId != e.Insert.AutoId)
            {
                e.Message = String.Format("编码必须唯一,编码{0}与单据{1}的编码重复", combination.AutoId, e.Insert.Code);
                e.Cancel = true;
                return;
            }
            DateTime dt1, dt2;
            dt1 = dt2 = DateTime.Now;
            DateTime.TryParse(e.Insert.OpenDate, out dt1);
            DateTime.TryParse(e.Insert.DueDate, out dt2);
            if (dt1 > dt2)
            {
                e.Message = String.Format("单据时间检验错误，失效时间{0}必须大于生效时间{1}", e.Insert.OpenDate, e.Insert.DueDate);
                e.Cancel = true;
                return;
            }
        }
    }
}
